import 'package:flutter/material.dart';
import 'package:flutter_09_game/config/app_color.dart';
import 'package:flutter_09_game/generated/assets.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/pages/profile/profile_bet_record/models/model_game_round_join.dart';
import 'package:flutter_09_game/utils/extensions/num_ext.dart';
import 'package:flutter_09_game/utils/log/utils_log.dart';
import 'package:flutter_09_game/utils/utils_other.dart';
import 'package:flutter_09_game/widgets/image/auto_image.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

/// 游戏统计组件
/// 显示TRX和USDT的投注统计数据，严格按照Figma设计图实现
class WidgetGameStats extends StatelessWidget {
  final List<ModelGameRoundJoinStats> stats;
  final VoidCallback? onRefresh;

  // final bool showUSDT;
  // final bool showTRX;

  const WidgetGameStats({
    super.key,
    required this.stats,
    this.onRefresh,

    // this.showUSDT = true,
    // this.showTRX = true
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(12.w),
      decoration: BoxDecoration(
        color: AppColor.homeTabBackground,
        borderRadius: BorderRadius.circular(12.w),
      ),
      child: Column(
        children: [
          // 表头
          _buildHeader(),
          12.verticalSpaceFromWidth,
          Column(
            children: stats.map((item) {
              return _buildDataRow(item);
            }).toList(),
          )
          // 数据行
          // _buildDataRows(),
        ],
      ),
    );
  }

  /// 构建表头
  Widget _buildHeader() {
    return Row(
      children: [
        // 币种
        SizedBox(
          width: 70.w,
          child: Text(
            LocaleKeys.currency.tr,
            style: TextStyle(
              fontSize: 12.sp,
              color: AppColor.textPrimary.withOpacity(0.94),
              fontWeight: FontWeight.w400,
            ),
            textAlign: TextAlign.center,
          ),
        ),
        4.horizontalSpace,
        // 总投注（单）
        Expanded(
          child: Text(
            LocaleKeys.totalBet.tr,
            style: TextStyle(
              fontSize: 12.sp,
              color: AppColor.textPrimary.withOpacity(0.7),
              fontWeight: FontWeight.w400,
            ),
            textAlign: TextAlign.center,
          ),
        ),
        4.horizontalSpace,
        // 有效投注
        Expanded(
          child: Text(
            LocaleKeys.validBet.tr,
            style: TextStyle(
              fontSize: 12.sp,
              color: AppColor.textPrimary.withOpacity(0.7),
              fontWeight: FontWeight.w400,
            ),
            textAlign: TextAlign.center,
          ),
        ),
        4.horizontalSpace,
        // 输赢总计
        Expanded(
          child: Text(
            LocaleKeys.totalWinLose.tr,
            style: TextStyle(
              fontSize: 12.sp,
              color: AppColor.textPrimary.withOpacity(0.7),
              fontWeight: FontWeight.w400,
            ),
            textAlign: TextAlign.center,
          ),
        ),
      ],
    );
  }

  /// 构建数据行
  // Widget _buildDataRows() {
  //   // ModelGameRoundJoinStats? statDat
  //   // final statData = (stats ?? []).length == 1
  //   //     ? stats?.first
  //   //     : stats?.firstWhereOrNull((item) => symbol == item.symbol);
  //   return Column(
  //     children: [
  //       // USDT行
  //       if (showUSDT)
  //         _buildDataRow(
  //           'USDT',
  //         ),
  //       8.verticalSpaceFromWidth,
  //       // TRX行
  //       if (showTRX)
  //         _buildDataRow(
  //           'TRX',
  //         ),
  //     ],
  //   );
  // }

  /// 构建单个数据行
  Widget _buildDataRow(
    ModelGameRoundJoinStats statData,
  ) {
    // 计算显示数据
    final totalBets = statData.count ?? 0;
    final validBets = statData.amount ?? 0;
    int winAmount;
    // if (statData.loseAmount != null) {
    //   winAmount = (statData.winAmount ?? 0) > 0
    //       ? (statData.winAmount ?? 0)
    //       : -(statData.loseAmount ?? 0);
    // } else {
      winAmount = (statData.winAmount ?? 0) - validBets;
    // }

    return Container(
      padding: EdgeInsets.symmetric(vertical: 4.w),
      child: Row(
        children: [
          // 币种图标和名称
          SizedBox(
            width: 70.w,
            child: _buildCoinInfo(statData.symbol ?? ''),
          ),
          4.horizontalSpace,
          // 总投注（单）
          Expanded(
            child: Text(
              totalBets.toString(),
              style: TextStyle(
                fontSize: 14.sp,
                color: AppColor.textPrimary.withOpacity(0.94),
                fontWeight: FontWeight.w600,
              ),
              textAlign: TextAlign.center,
            ),
          ),
          4.horizontalSpace,
          // 有效投注
          Expanded(
            child: Text(
              validBets.formatWallet(),
              style: TextStyle(
                fontSize: 14.sp,
                color: AppColor.textPrimary.withOpacity(0.94),
                fontWeight: FontWeight.w600,
              ),
              textAlign: TextAlign.center,
            ),
          ),
          4.horizontalSpace,
          // 输赢总计
          Expanded(
            child: Text(
              _formatWinAmount(winAmount),
              style: TextStyle(
                fontSize: 14.sp,
                color: _getWinAmountColor(winAmount, statData),
                fontWeight: FontWeight.w600,
              ),
              textAlign: TextAlign.center,
            ),
          ),
        ],
      ),
    );
  }

  /// 构建币种信息（图标+名称）
  Widget _buildCoinInfo(
    String symbol,
  ) {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        AutoImage(
          UtilsOther().getAmountTagIcon(symbol),
          width: 20.w,
        ),

        4.horizontalSpace,
        // 币种名称
        Expanded(
          child: Text(
            symbol,
            style: TextStyle(
              fontSize: 14.sp,
              color: AppColor.textPrimary.withOpacity(0.94),
              fontWeight: FontWeight.w600,
            ),
            textAlign: TextAlign.left,
          ),
        ),
      ],
    );
  }

  /// 格式化盈亏金额显示
  String _formatWinAmount(int amount) {
    if (amount == 0) return '0.00';
    // clog('dalsjdlsajdkl   $amount');
    if (amount > 0) {
      return '+${amount.formatWallet()}';
    } else {
      return amount.formatWallet();
    }

    // return 1231237.formatWallet();
    // return ';';
  }

  /// 获取盈亏金额颜色
  Color _getWinAmountColor(
    int amount,
    ModelGameRoundJoinStats statData,
  ) {
    if (amount > 0) {
      return AppColor.homePrizePoolIndicator; // 绿色 - 盈利
    }
    // if (amount == 0 && statData.loseAmount != null) {
    //   return AppColor.colorEDA247; // 绿色 - 盈利
    // } else {
      return AppColor.gameResultEven; // 红色 - 亏损
    // }
  }
}
